package com.wss.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wss.merchant.domain.DlNewProduct;
import com.wss.merchant.domain.DlNewProductspecs;
import com.wss.merchant.domain.DlNewTypes;
import com.wss.merchant.mapper.DlNewProductMapper;
import com.wss.merchant.mapper.DlNewProductspecsMapper;
import com.wss.merchant.mapper.DlNewTypesMapper;
import com.wss.merchant.service.DlNewProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品表 服务实现类
 * </p>
 *
 * @author wss
 * @since 2021-02-23
 */
@Service
public class DlNewProductServiceImpl extends ServiceImpl<DlNewProductMapper, DlNewProduct> implements DlNewProductService {
    @Resource
    private DlNewProductMapper dlNewProductMapper;
    @Resource
    private DlNewProductspecsMapper dlNewProductspecsMapper;
    @Resource
    private DlNewTypesMapper dlNewTypesMapper;
    @Override
    public Page<Map> selectProduct(IPage<Map> page, Map map) {

        return dlNewProductMapper.selectProduct(page,map);
    }

    @Override
    @Transactional
    public boolean alterProduct(Map map) {

//        try {
            //商品表id
            Integer id = (Integer) map.get("id");
            //产品规格表id
            Integer prid = (Integer) map.get("prid");

            //商品名称
            String pname = (String) map.get("pname");
            //商品类型
            String types = (String) map.get("types");
            //商品规格名称
            String prname = (String) map.get("prname");
            //价格 sales
            String fee = String.valueOf(map.get("fee"));

//            库存
            Integer sales = Integer.parseInt(map.get("sales").toString());
//            String sales = (String) map.get("sales");

//            销量
//            String stock = (String) map.get("stock");
            Integer stock = Integer.parseInt(map.get("stock").toString());

            //介绍
            String describes = (String) map.get("describes");

            //产品
            DlNewProduct dlNewProduct = dlNewProductMapper.selectById(id);
            //产品类型
            DlNewProductspecs dlNewProductspecs = dlNewProductspecsMapper.selectById(prid);
            //类型id
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("name", types);
            Integer id1 = dlNewTypesMapper.selectOne(queryWrapper).getId();
            dlNewProduct.setSales(Integer.valueOf(sales));
            dlNewProduct.setName(pname);
            dlNewProduct.setTypes(id1);
            dlNewProduct.setDescribes(describes);

            int i = dlNewProductMapper.updateById(dlNewProduct);
            dlNewProductspecs.setName(prname);
            dlNewProductspecs.setFee(fee);
            dlNewProductspecs.setStock(Integer.valueOf(stock));


//            dlNewProductspecs.setStock(sales);
            int i1 = dlNewProductspecsMapper.updateById(dlNewProductspecs);
            return true;

//        }catch (Exception e){
//            return false;
//        }








    }

    @Override
    public Page<DlNewProduct> getStoreProduct(IPage<Map> page, Map map) {
        return dlNewProductMapper.getStoreProduct(page,map);
    }
}
